from rest_framework.generics import ListAPIView, ListCreateAPIView
from meiduo_admin.serializers.users import UserSerializer
from rest_framework.permissions import IsAdminUser
from users.models import User
from meiduo_admin.utils import PageNum
# from meiduo_mall.apps.meiduo_admin.serializers.users import UserSerializer


class UserView(ListCreateAPIView):
#指定查询集
    queryset = User.objects.filter(is_staff=False)
# 指定序列化器
    serializer_class = UserSerializer
# 指定分页器
    pagination_class = PageNum
#指定权限
    permission_classes = [IsAdminUser]
    # keyword父类重写
    def get_queryset(self):
        keyword = self.request.query_params.get('keyword')

        if keyword is None or keyword == '':
            return User.objects.filter(is_staff=False)

        else:
            return User.objects.filter(is_staff=False, username__contains=keyword)

